<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="root">
        <div>
            <input v-model="content" type="text"/>
            <button @click="addData">提交</button>
        </div>
        <ul->
            <!-- @delete="remove" 父组建监听子组建delete事件，如果delete事件被触发，父组件带着delete事件传递过来的参数值并触发remove事件   -->
            <li-items v-for="(item, index) in list" :key="index" :value="item" :index="index" @delete="remove"></li-items>
        </ul->
    </div>
    <script>
        // 全局组建
        Vue.component('li-items',{
            props: ['value','index'],
            // 设置li-items组件的触发事件
            template: '<li @click="del">{{value}}</li>',
            methods: {
                del: function(){
                     //向外触发事件delete 并携带了下标值
                     this.$emit('delete',this.index)   
                }
            }
        })

        //局部组建
        // var todoItem = {
        //     template: '<li>item</li>'
        // }

       new  Vue({
           el: '#root',
        //    components: { 引入局部组件
        //       'li-items': todoItem
        //    },
           data: {
            content: '' ,
            list: []
           },
           methods: {
             addData: function(){
                 this.list.push(this.content);
                 this.content = '';
             },
             remove: function(index){
                this.list.splice(index,1);
             }
           }
       })  


    </script>
</body>
</html>